package com.unity3d.player;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class PrivacyActivity_Google extends Activity implements DialogInterface.OnClickListener {
    boolean useLocalHtml = true;
    String privacyUrl = "https://note.youdao.com/s/1wM3hJq8";
    final String htmlStr = "    Welcome to use this game. Before using this game, please fully read and understand the terms of the <a href=\"https://note.youdao.com/s/RWyTezif\">Privacy Policy</a>, understand our rules for handling personal information, and the purpose of permission applications. We remind you to pay attention to the relevant clauses in the aforementioned agreement regarding our exemption from personal responsibility, limitation of your rights, dispute resolution methods, judicial jurisdiction, and other content. We will strictly comply with relevant laws, regulations, and privacy policies to protect your personal privacy. To ensure your gaming experience, we will apply for the following necessary permissions from you, and you can choose to agree or refuse. Refusal may result in the inability to enter this game. At the same time, we will apply for non essential permissions from you based on the specific needs of the relevant functions in this game. You can choose to agree or refuse, as refusal may lead to abnormal gaming experience. The necessary permissions include: device permissions (necessary): reading unique device identifiers (Android ID, Mac), generating accounts, saving and restoring game data, identifying abnormal states, and ensuring network and operational security. Storage permissions (necessary): Access your storage space so that you can download and save content, store and upload images, cache personal information for reading and writing, and create system and log files.\n";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityInfo actInfo = null;
        try {
            //获取AndroidManifest.xml配置的元数据
            actInfo = this.getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
            useLocalHtml = actInfo.metaData.getBoolean("useLocalHtml");
            privacyUrl = actInfo.metaData.getString("privacyUrl");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        //如果已经同意过隐私协议则直接进入Unity Activity
        if (GetPrivacyAccept()){
            EnterUnityActivity();
            return;
        }
        ShowPrivacyDialog();//弹出隐私协议对话框
    }

    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        switch (i){
            case AlertDialog.BUTTON_POSITIVE://点击同意按钮
                SetPrivacyAccept(true);
                EnterUnityActivity();//启动Unity Activity
                break;
            case AlertDialog.BUTTON_NEGATIVE://点击拒绝按钮,直接退出App
                finish();
                break;
        }
    }
    private void ShowPrivacyDialog(){
        WebView webView = new WebView(this);
        if (useLocalHtml){
            webView.loadDataWithBaseURL(null, htmlStr, "text/html", "UTF-8", null);
        }else{
            webView.loadUrl(privacyUrl);
            webView.setWebViewClient(new WebViewClient(){
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }

                @Override
                public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                    view.reload();
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                }
            });
        }

        AlertDialog.Builder privacyDialog = new AlertDialog.Builder(this);
        privacyDialog.setCancelable(false);
        privacyDialog.setView(webView);
        privacyDialog.setTitle("Privacy statement and User agreement");
        privacyDialog.setNegativeButton("Cancel",this);
        privacyDialog.setPositiveButton("Confirm",this);
        privacyDialog.create().show();
    }
    //启动Unity Activity
    private void EnterUnityActivity(){
        Intent unityAct = new Intent();
        unityAct.setClassName(this, "com.unity3d.player.UnityPlayerActivity");
        this.startActivity(unityAct);
    }
    //保存同意隐私协议状态
    private void SetPrivacyAccept(boolean accepted){
        SharedPreferences.Editor prefs = this.getSharedPreferences("PlayerPrefs", MODE_PRIVATE).edit();
        prefs.putBoolean("PrivacyAccepted", accepted);
        prefs.apply();
    }
    private boolean GetPrivacyAccept(){
        SharedPreferences prefs = this.getSharedPreferences("PlayerPrefs", MODE_PRIVATE);
        return prefs.getBoolean("PrivacyAccepted", false);
    }
}
